package com.cloud.core.multistate;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.cloud.core.R;
import com.cloud.widget.utils.DensityUtils;
import com.dylanc.loadinghelper.LoadingHelper;

import org.jetbrains.annotations.NotNull;

import kotlin.jvm.functions.Function1;

/**
 * Created by Chen on 2020/10/22.
 * Description：
 */
public class ToolbarAdapter extends LoadingHelper.Adapter<ToolbarAdapter.ViewHolder> {

    private String title;
    private NavIconType type;
    private int menuId;
    private Function1<? super MenuItem, Boolean> onMenuItemClick;

    public ToolbarAdapter(String title, NavIconType type) {
        this.title = title;
        this.type = type;
    }

    public ToolbarAdapter(String title, NavIconType type, int menuId, Function1<? super MenuItem, Boolean> onMenuItemClick) {
        this.title = title;
        this.type = type;
        this.menuId = menuId;
        this.onMenuItemClick = onMenuItemClick;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull LayoutInflater inflater, @NotNull ViewGroup parent) {
        return new ToolbarAdapter.ViewHolder(inflater.inflate(R.layout.layout_toolbar, parent, false));
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {       //5.0以上(包含)
            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            holder.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
            holder.getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {    //4.3以上(不包含)
            holder.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            holder.state.setBackgroundResource(R.color.white);
            ViewGroup.LayoutParams lp =  holder.state.getLayoutParams();
            lp.height = DensityUtils.getStatusBarHeight(holder.getActivity());
            holder.state.setLayoutParams(lp);
            holder.state.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(title)) {
            holder.toolbar.setTitle(title);
        }

        if (type == NavIconType.BACK) {
            holder.toolbar.setNavigationIcon(R.mipmap.icon_back);
            holder.toolbar.setNavigationOnClickListener(v -> holder.getActivity().finish());
        } else {
            holder.toolbar.setNavigationIcon(null);
        }

        if (menuId > 0 && onMenuItemClick != null) {
            holder.toolbar.inflateMenu(menuId);
            holder.toolbar.setOnMenuItemClickListener(item -> onMenuItemClick.invoke(item));
        }
        holder.toolbar.clearFocus();
    }

    static class ViewHolder extends LoadingHelper.ViewHolder {

        private final Toolbar toolbar;
        private final View state;

        ViewHolder(@NonNull View rootView) {
            super(rootView);
            toolbar = rootView.findViewById(R.id.toolbar);
            state = rootView.findViewById(R.id.id_view_state);
        }

        private Activity getActivity() {
            Context context = getRootView().getContext();
            return (Activity) getRootView().getContext();
        }
    }
}
